package com.ntanh.mealplanner.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.ntanh.mealplanner.R;
import com.ntanh.mealplanner.main.custom_adapter.RecipeAdapter;
import com.ntanh.mealplanner.main.dao.DataBaseHelper;
import com.ntanh.mealplanner.main.dao.RecipeDao;
import com.ntanh.mealplanner.main.objects.Recipe;

import java.util.ArrayList;

public class FragmentRecipe extends Fragment {

	public static final Integer FRAGMENT_ICON = R.drawable.ic_recipe;
	public static final Integer FRAGMENT_NAME = R.string.recipe;
    RecipeDao recipeDao;
	ImageView ivIcon;
	TextView tvItemName;
    RecipeAdapter listViewItemAdapter;
	public static final String IMAGE_RESOURCE_ID = "iconResourceID";
	public static final String ITEM_NAME = "itemName";

    ListView lvRecipeList;
    ArrayList<Recipe> recipeLists;
	public FragmentRecipe() {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		final View view = inflater.inflate(R.layout.fragment_layout_recipe, container, false);

		ivIcon = (ImageView) view.findViewById(R.id.frag2_icon);
		tvItemName = (TextView) view.findViewById(R.id.frag2_text);

		tvItemName.setText(view.getResources().getString(FRAGMENT_NAME));
		ivIcon.setImageDrawable(view.getResources().getDrawable(FRAGMENT_ICON));

        recipeDao = new RecipeDao(getActivity());
        recipeLists = recipeDao.getAll("author", false);
        lvRecipeList = (ListView) view.findViewById(R.id.recipeListView);
        listViewItemAdapter = new RecipeAdapter(getActivity(), R.layout.recipe_item, recipeLists);
        lvRecipeList.setAdapter(listViewItemAdapter);

        Button addNewRecipe = (Button) view.findViewById(R.id.newRecipeButton);
        addNewRecipe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView newRecipeName = (TextView) view.findViewById(R.id.newRecipeName);
                String newRecipeValue = newRecipeName.getText().toString();
                if (newRecipeValue.isEmpty()) {
                    return;
                }

                Recipe recipe = new Recipe("author", newRecipeValue);
                if (recipeDao.save(recipe)) {
                    recipeLists.add(recipe);
                    listViewItemAdapter.notifyDataSetChanged();
                    newRecipeName.setText("");
                }
            }
        });

        registerForContextMenu(lvRecipeList);
        return view;
	}
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.menu_recipe_item, menu);
    }
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case R.id.menu_recipe_item_remove:
                Recipe recipe = recipeLists.get(info.position);
                removeRecipe(recipe);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }
    public void removeRecipe(Recipe recipe){
        if(recipeDao.delete(recipe) > 0){
            recipeLists.remove(recipe);
            listViewItemAdapter.notifyDataSetChanged();
        }
        return;
    }
}